package com.webserver.core;

import com.webserver.http.HttpRequest;

import java.io.*;
import java.net.Socket;


/**
 * 处理与指定客户端的一次HTTP交互
 * 完成一次交互由三步构成：
 * 1.解析请求
 * 2.处理请求
 * 3.响应客户端请求
 *
 * */

public class Clienthandler implements Runnable {
    private Socket socket;
    public Clienthandler(Socket socket){
        this.socket = socket;
    }
    public void run(){
        try {

            //1.解析请求
            HttpRequest request = new HttpRequest(socket);


            //2.处理请求
            String path = request.getUri();
            File file = new File("./webapps"+path);
            if (file.exists()&&file.isFile()) {
                //3.发送响应
            /*
                HTTP/1.1 200 OK(CRLF)
                Content-Type: text/html(CRLF)
                Content-Length: 2546(CRLF)(CRLF)
                1011101010101010101......

            */
                OutputStream out = socket.getOutputStream();

                //3.1:发送状态行
                String line = "HTTP/1.1 200 OK";
                byte[] data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                //3.2发送响应头
                line = "Content-type: txet/html";
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                line = "Content-length: "+file.length();
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                //单独发送CRLF表示响应头发送完毕
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                //3.3:发送响应正文
                FileInputStream fis = new FileInputStream(file);
                data = new byte[1024*10];//10kb
                int len;//每次实际读取到的字节数量
                while ((len=fis.read(data))!=-1){
                    out.write(data,0,len);
                }
                System.out.println("响应发送完毕！");

            } else {
                String path1 = "root/404.html";
                File file1 = new File("./webapps/"+path1);
                OutputStream out = socket.getOutputStream();

                //3.1:发送状态行
                String line = "HTTP/1.1 404 NotFound";
                byte[] data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                //3.2发送响应头
                line = "Content-type: text/html";
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                line = "Content-length: "+file1.length();
                data = line.getBytes("ISO8859-1");
                out.write(data);
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                //单独发送CRLF表示响应头发送完毕
                out.write(13);//发送一个回车符
                out.write(10);//发送一个换行符

                //3.3:发送响应正文
                FileInputStream fis = new FileInputStream(file1);
                data = new byte[1024*10];//10kb
                int len;//每次实际读取到的字节数量
                while ((len=fis.read(data))!=-1){
                    out.write(data,0,len);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //一次HTTP交互完毕后要与客户端断开连接（HTTP协议要求）
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
